package com.xilihui.xlh.business.activitys.store;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.RefundEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/10/8.
 * 退款 状态
 */

public class AlterationStatusActivity extends BaseCompatActivity{
    
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_syTime)
    TextView tv_syTime;
    
    @BindView(R.id.tv_shopName)
    TextView tv_shopName;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_address)
    TextView tv_address;
        
    @BindView(R.id.sdv)
    SimpleDraweeView sdv;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_model)
    TextView tv_model;
    
    @BindView(R.id.tv_tuikuangjiner)
    TextView tv_tuikuanjiner;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_bianhao)
    TextView tv_bianhao;
    
    @BindView(R.id.cl_address)
    ConstraintLayout cl_address;
    @BindView(R.id.ll_tuikuang)
    LinearLayout ll_tuikuang;
    
    @BindView(R.id.tv_jiner)
    TextView tv_jiner;
    @BindView(R.id.tv_tkfs)
    TextView tv_tkfs;
    
    @BindView(R.id.tv_revocation)
    TextView tv_revocation;
    @BindView(R.id.cl_status)
    ConstraintLayout cl_status;
    
    
    String order_id = "";
    String status = "";
    
    String link_phone = "";
    String type = "";
    
    String id = "";
    
    @Override
    public int getLayout() {
        return R.layout.activity_alteration_status;
    }

    @Override
    public String initTitle() {
        return "退货退款";
    }
    
    
    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {

        order_id = getIntent().getStringExtra("order_id");
        status = getIntent().getStringExtra("status");
        
    }
            
    @Override
    public void initData() {
        getData();
    }

    public void getData(){
        
        StoreRequest.returnGoods(this,order_id)
                .compose(DoTransform.<RefundEntity>applyScheduler(this,true))
                .compose(this.<RefundEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<RefundEntity>(this) {
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    protected void onSuccess(RefundEntity entity) {
                        
                        id = entity.getInfo().getId();
                        
                        type = entity.getInfo().getRefund_type();
                        String order_status = entity.getInfo().getOrder().getOrder_status();
                        
                        if(type.equals("1")){//退款退货
                            setTitle("退款退货");
                            
                            if(order_status.equals("-1")){//审核未通过
                                tv_revocation.setText("重新审核");
                            }else if(order_status.equals("0")){//待审核
                                
                            }else if(order_status.equals("1")){//审核通过
                                cl_address.setVisibility(View.VISIBLE);
                                tv_revocation.setVisibility(View.GONE);
                            }else if(order_status.equals("3")){//审核成功
                                cl_address.setVisibility(View.GONE);
                                ll_tuikuang.setVisibility(View.VISIBLE);
                                tv_revocation.setVisibility(View.GONE);
                            }
                        }else if (type.equals("0")){//仅退款
                            setTitle("仅退款");
                            
                            if(order_status.equals("-1")){//审核未通过
                                ll_tuikuang.setVisibility(View.GONE);
                                tv_revocation.setText("重新申请");
                                Drawable drawable = getResources().getDrawable(R.drawable.share_tv_bg_black);
                                cl_status.setBackground(drawable);
                            }else if(order_status.equals("0")){//待审核
                                ll_tuikuang.setVisibility(View.GONE);
                            }else if(order_status.equals("1")){//审核通过
                                ll_tuikuang.setVisibility(View.VISIBLE);
                                tv_revocation.setVisibility(View.GONE);
                            }else if(order_status.equals("3")){//审核成功
                                cl_address.setVisibility(View.GONE);
                                ll_tuikuang.setVisibility(View.VISIBLE);
                                tv_revocation.setVisibility(View.GONE);
                                ll_tuikuang.setVisibility(View.GONE);
                            }
                            
                            cl_address.setVisibility(View.GONE);
                            
                        }
                        
                        tv_status.setText(entity.getInfo().getTitle());
                        tv_syTime.setText(entity.getInfo().getSubtitle());
                        
                        tv_shopName.setText("收件人："+entity.getInfo().getStore().getContacts());
                        tv_phone.setText(entity.getInfo().getStore().getPhone());
                        tv_address.setText("收货地址："+entity.getInfo().getStore().getAddress());
                        
                        sdv.setImageURI(UrlConst.baseUrl()+ entity.getInfo().getOrder_goods().getOriginal_img());
                        tv_title.setText(entity.getInfo().getOrder_goods().getGoods_name());
                        tv_model.setText(entity.getInfo().getOrder_goods().getSpec_key_name());
                        
                        tv_tuikuanjiner.setText("￥"+entity.getInfo().getOrder().getOrder_amount());
                        tv_time.setText(entity.getInfo().getAdd_time());
                        tv_bianhao.setText(entity.getInfo().getOrder_sn());
                        
                        link_phone = entity.getPhone();
                        
                        tv_jiner.setText("￥"+entity.getInfo().getOrder().getOrder_amount());
                        tv_tkfs.setText("原路返还至"+entity.getInfo().getOrder().getPay_name());
                        
                        
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                    
                    
                });
        
    }

    /**
     * 撤销退款
     */
    @OnClick(R.id.tv_revocation)
    public void revocation(){
        
        StoreRequest.cancelRefund(this,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        YEventBuses.post(new YEventBuses.Event("revocation"));
                        ToastUtil.toastShortPositive(entity.getMsg());
                        finish();
                    }
                                     
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }

    /**
     * 联系客服
     */
    @OnClick(R.id.tv_link)
    public void link(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + link_phone);
        intent.setData(data);
        startActivity(intent);
    }
    
}
